
capture log close
log using $log/EDS_04_hetoregeneity, text replace

clear
clear matrix
clear mata
set mem 2g
set matsize 800
set more off
set maxvar 20000
set trace off
set varabbrev off


	  #d;
	 
	   
	   
		#d;
		global outc"
		   search_dist50 
	      applications_total
		   applications_distant
		   log_distance_km 
		    ";
	  #d cr
	  
	use $data/mobihilfen_all,clear

gen share_relocation = umzug/fahrkosten

replace year=year+1

rename ba region
keep region share_relocation year
tempfile intensity
save `intensity'
  
	  
	  use $data/data_pair, clear
	
	joinby region year using  `intensity',unmatched(master)
tab _merge
drop _merge

sum share_relocation, d

gen rel_reg=(share_relocation>r(p50))
	  

	   tab ref_ue_m, g(entry_)
	 
sum pred_relocation,d
gen group3=(pred_relocation>r(p75))
gen group2=female	  
gen group1=rel_reg	  
	
	
	bys pers_id: gen in_sample = (_n==1)
	
		
		forvalues group = 1/3 {
		
		if (`group'==1|`group'==3) {
		 #d;
		 global cov " 
		 _Isu_schule_1 _Isu_schule_2 _Isu_schule_3 
	_Isu_ausbila1 _Isu_ausbila2
	_Isu_kat_al_2 _Isu_kat_al_3 _Isu_kat_al_4 
	_Ichild_1 _Ichild_2
	_Isu_eintri_1 _Isu_eintri_2 _Isu_eintri_3 _Isu_eintri_4 _Isu_eintri_5
	_Isu_eintri_6 _Isu_eintri_7 _Isu_eintri_8 _Isu_eintri_9 _Isu_eintri_10 _Isu_eintri_11
	_Ibefstat_1 _Ibefstat_2 _Ibefstat_3 _Ibefstat_4
	_Idauloint__8 _Idauloint__9 _Idauloint__10 _Idauloint__11 _Idauloint__12 
	_Idauloint__13 _Idauloint__14
	female east_germany
    german migration_background 
    married  
	benefit_status c.benefit_level c.log_benefit_level
    c.lifetime_ue_age c.lifetime_empl_age c.last_wage last_wage_mis 
    c.region_ba_aloq_zivall c.gdp_real_pc c.region_ba_vacancies 
	c.sector_manufacturing c.sector_service  c.log_mobirate_avg5
	c.openness_st c.conscient_st c.extraversion_st c.neuroticism_st c.loc_st
	write_ger speak_ger write_eng speak_eng
	homeowner
		  ";
	   #d cr
		}
		
		if (`group'==2) {
		
		 #d;
		 global cov "	 _Isu_schule_1 _Isu_schule_2 _Isu_schule_3 
	_Isu_ausbila1 _Isu_ausbila2
	_Isu_kat_al_2 _Isu_kat_al_3 _Isu_kat_al_4 
	_Ichild_1 _Ichild_2
	_Isu_eintri_1 _Isu_eintri_2 _Isu_eintri_3 _Isu_eintri_4 _Isu_eintri_5
	_Isu_eintri_6 _Isu_eintri_7 _Isu_eintri_8 _Isu_eintri_9 _Isu_eintri_10 _Isu_eintri_11
	_Ibefstat_1 _Ibefstat_2 _Ibefstat_3 _Ibefstat_4
	_Idauloint__8 _Idauloint__9 _Idauloint__10 _Idauloint__11 _Idauloint__12 
	_Idauloint__13 _Idauloint__14
	east_germany
    german migration_background 
    married  
	benefit_status c.benefit_level c.log_benefit_level
    c.lifetime_ue_age c.lifetime_empl_age c.last_wage last_wage_mis 
    c.region_ba_aloq_zivall c.gdp_real_pc c.region_ba_vacancies 
	c.sector_manufacturing c.sector_service  c.log_mobirate_avg5
	c.openness_st c.conscient_st c.extraversion_st c.neuroticism_st c.loc_st
	write_ger speak_ger write_eng speak_eng
	homeowner
	   ";
	   #d cr
		}
		if (`group'==1) {
		local name "LEA_reloc"
		local name1 "Commuting"
		local name2 "Relocation"
		}
		
		if (`group'==2) {
		local name "gender"
		local name1 "Men"
		local name2 "Women"
		}
		
		if (`group'==3) {
		local name "exp_reloc"
		local name1 "Not top quartile"
		local name2 "Top quartile"
		}
		tostring group`group', replace
		gen region_pair_int = substr(group`group',1,1) + substr(region_pair,1,6)
		destring group`group', replace
	
		gen log_intensity_int = log_intensity_alt*group`group'
	
		encode region_pair_int, g(region_pair2)
		
		
		
		
		
		foreach lhs in  $outc {
		
		qui reg `lhs' log_intensity_alt $cov  county_pair_*  [pw=1/fweight_reg] if group`group'==0, cluster(region)
		
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		count if e(sample)==1 & in_sample==1
		matrix n=r(N) 
		matrix d1=b[1..1,1..1],se,n
		
		
		qui reg `lhs' log_intensity_alt $cov  county_pair_*  [pw=1/fweight_reg] if group`group'==1, cluster(region)
		
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		count if e(sample)==1 & in_sample==1
		matrix n=r(N) 
		matrix d2=b[1..1,1..1],se,n
		
		
		qui areg `lhs'  log_intensity_alt log_intensity_int group`group' (group`group')##($cov) [pw=1/fweight_reg], cluster(region) absorb(region_pair2)
		 matrix r=r(table)
		 scalar p_int = r[4,2]
		 
		 matrix d_`lhs' = d1,d2,p_int
		
		}
		
		
		
		
		#delimit;
		matrix d =
		  d_search_dist50 \
		  d_log_distance_km \
	      d_applications_total \
		  d_applications_distant 
		  
		;
	
	matrix rownames d = 
	
	"Applied to distant"
	"Search radius"  
	"#applications total" 
	"#applications distant" 
	;
	#delimit cr

	matrix colname d = "group 1" "SE" "Obs" "group 2" "SE" "Obs" "P-value"
	mat list d
	estout matrix(d, fmt(4)) using $data/Table_4_survey_`name'.tex, style(tex) replace
	drop region_pair_int log_intensity_int region_pair2 
}
     
